package cn.entergx.smallmengplan.widget

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent


class MyImageView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatImageView(context, attrs, defStyleAttr) {
    private var lastY: Float = 0f
    private var offset = 0
    private var rawY = 0
    private var i = true
    private val t = 200
    private var onSlideListener: OnSlideListener? = null

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (i) {
            rawY = top
            i = false
        }
        if (event != null) {
            val y = event.y
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    // 记录触摸点坐标
                    lastY = y
                }
                MotionEvent.ACTION_MOVE -> {
                    // 计算偏移量
                    val offsetY = y - lastY
                    offset = rawY - top
                    println("---$y")
                    if (offset >= t) {
                        // 在当前left、top、right、bottom的基础上加上偏移量

                        layout(left, rawY - t, right, rawY + height - t)
                    } else
                        layout(left, (top + offsetY).toInt(), right, (bottom + offsetY).toInt())
                }
                MotionEvent.ACTION_UP -> {
                    if (offset == t)
                        onSlideListener?.onSlide()
                    layout(left, rawY, right, rawY + height)
                }
            }
        }
        return true
    }


    interface OnSlideListener {
        fun onSlide()
    }

    fun setOnSlideListener(onSlideListener: OnSlideListener) {
        this.onSlideListener = onSlideListener
    }
}